// pages/gjIndex/cxOrder/cxOrder.js
import config from "../../../config.js";
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [],
    typeIndex: 0,
    user: null,
    car: null,
    userId: null,
    orderId: -1,
    order: null,
    canChange: 1,
    //行驶证
    licenseFr: "https://app.longchg.com/gudongcar/images/licenseA.png",
    licenseBa: "https://app.longchg.com/gudongcar/images/licenseB.png",
    //身份证
    idenCardA: "https://app.longchg.com/gudongcar/images/idenCardA.png",
    idenCardB: "https://app.longchg.com/gudongcar/images/idenCardB.png",
    isUploadFr: false,
    isUploadBa: false,
    isUploadIDFr: false,
    isUploadIDBa: false,
    isUploadFirst: true,
    isUploadBaFirst: true,
    isUploadIDFirst: true,
    isUploadBaIDFirst: true,
  },

  typeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value,
    })
    wx.setStorageSync("comId", this.data.typeList[this.data.typeIndex].insuranceId);
    console.log(this.data.typeList[this.data.typeIndex].insuranceId);
  },
  select: function(e) {
    this.setData({
      index: e.detail.value
    })
  },

  servType: function(e) {
    this.setData({
      typeInndex: e.detail.value
    })
  },

  selectCustomer: function() {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },

  submit: function() {

    if (this.data.car == null) {
      wx.showToast({
        title: '请选择用户车辆',
        icon: 'none',
      })
      return;
    }
    wx.setStorageSync("orderConfirmType", 1);
    wx.setStorageSync("carchoose", this.data.car);
    wx.setStorageSync("userchoose", this.data.user);
    wx.setStorageSync("idenCardA", this.data.idenCardA);
    wx.setStorageSync("idenCardB", this.data.idenCardB);
    wx.setStorageSync("licenseFr", this.data.licenseFr);
    wx.setStorageSync("licenseBa", this.data.licenseBa);


    if (this.data.order != null) {
      wx.setStorageSync("againPlan", this.data.order.service.insuranceItemOrders);
    }

    var user = wx.getStorageSync("user");
    if (this.data.orderId != -1) {
      console.log(this.data.orderId + "again:");
      wx.navigateTo({
        url: "../../insurPlan/insurPlan?again=1&confirmType=2&orderId=" + this.data.orderId + "&keeperId=" + user.housekeeper.keeperId,
      })
    } else {
      console.log(this.orderId + "no:");


      console.log(user);
      wx.navigateTo({
        url: "../../insurPlan/insurPlan?again=0&confirmType=2&keeperId=" + user.housekeeper.keeperId,
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var canChange = options.canChange;
    var type = options.type;
    type = type == null ? "" : type;

    that.setData({
      canChange: canChange,
      type: type
    })

    this.getTypeList();

    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    var userId = options.uid;

    if (orderId != null) {
      that.setData({
        orderId: orderId
      })

      //获取订单详情
      that.orderDetail();
    }

  },


  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data,
            user: res.data.data.order.account,

          })
          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order;
    console.log(order);
    // var service = that.data.order.service;
    var car = order.order.cars;
    if (car != null) {
      that.setData({
        typeIndex: order.service.insuranceCom.insuranceId - 1,
        // user:order.account
        car: car,
        isUploadBaFirst: false,
        isUploadFirst: false,
        licenseFr: car.wdxlicensePic,
        licenseBa: car.wdxlicensePicB,

      })
    }


    var user = order.order.account;
    if (user.identityCardA != null && user.identityCardB != null) {
      //设置默认车辆图片信息
      that.setData({
        isUploadIDFirst: false,
        isUploadBaIDFirst: false,
        idenCardA: user.identityCardA,
        idenCardB: user.identityCardB,
        isUploadIDFr: true,
        isUploadIDBa: true,
        isUploadFr: true,
        isUploadBa: true,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var item = wx.getStorageSync("userchoose");
    console.log(item);
    if (item != null && item != "") {
      console.log(item);
      this.setData({
        user: item,
      })

      if (item.licenseA != null && item.licenseB != null) {
        //设置默认车辆图片信息
        console.log(item);
        that.setData({
          licenseFr: item.licenseA,
          licenseBa: item.licenseB,
          isUploadIDFr: true,
          isUploadIDBa: true,
          isUploadFr: true,
          isUploadBa: true,
          isUploadFirst: false,
          isUploadBaFirst: false
        })
      }

      that.getUserDetail(item.aid);
    }

    var car = wx.getStorageSync("carchoose");
    if (car != null && car != "") {
      //设置默认车辆图片信息
      this.setData({
        car: car,
        isUploadBaFirst: false,
        isUploadFirst: false,
        licenseFr: car.wdxlicensePic,
        licenseBa: car.wdxlicensePicB
      })
    }
  },
  getUserDetail: function(userId) {
    var that = this;
    wx.request({
      url: openIdUrl.getUserInfoById,
      data: {
        userId: userId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        var user = res.data.data;
        if (user.identityCardA != null && user.identityCardB != null) {
          //设置默认车辆图片信息
          that.setData({
            isUploadIDFirst: false,
            isUploadBaIDFirst: false,
            idenCardA: user.identityCardA,
            idenCardB: user.identityCardB,
            isUploadIDFr: true,
            isUploadIDBa: true,
            isUploadFr: true,
            isUploadBa: true,
          })
        }
      }
    })





  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("userchoose", null);
    wx.setStorageSync("carchoose", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  exchangeCar: function(e) {
    var userId = e.currentTarget.dataset.userid;
    if (userId) {
      wx.navigateTo({
        url: '../carList/carList?userId=' + userId,

      })
    }
  },
  exchangeCustomer: function(e) {
    var that = this;
    wx.navigateTo({
      url: '../customerList/customerList',
      success: function() {
        wx.setStorageSync("carchoose", null);
        that.setData({
          car: null,
          isUploadBaFirst: true,
          isUploadFirst: true,
          licenseFr: "../https://app.longchg.com/gudongcar/images/licenseA.png",
          licenseBa: "../https://app.longchg.com/gudongcar/images/licenseB.png",
        })
      }
    })
  },
  getTypeList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.insuranceComList,
      data: {
        start: 0,
        isNor: 1
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            typeList: res.data.data
          })
          wx.setStorageSync("comId", that.data.typeList[that.data.typeIndex].insuranceId);

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },
  selectCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  exchangeCar: function(e) {
    var id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../carList/carList?userId=' + id,
    })
  },
  //行驶证
  uploadFr: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseFr: tempFilePaths,
          isUploadFr: true,
          isUploadFirst: false
        })

        console.log(that.data.licenseFr);


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseFr[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadFr: true,
              })
              that.data.licenseFr = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBa: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseBa: tempFilePaths,
          isUploadBa: true,
          isUploadBaFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseBa[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadBa: true,
              })
              that.data.licenseBar = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //身份证正面
  uploadFrID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardA: tempFilePaths,
          isUploadIDFr: true,
          isUploadIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardA[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadIDFr: true,
              })
              that.data.idenCardA = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBaID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardB: tempFilePaths,
          isUploadIDBa: true,
          isUploadBaIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardB[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadIDBa: true,
              })
              that.data.idenCardB = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  }
})